package com.youlu.campus.service.vote;

import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.vote.VoteConstant;
import com.youlu.campus.entity.vote.VoteOrganizationInfo;
import com.youlu.campus.entity.vote.VoteSimpleActivityEntity;
import com.youlu.campus.entity.vote.dto.OrgInfoSearchDTO;
import com.youlu.campus.service.course.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author : [dengbo]
 * @className : VoteOrganizationInfo
 * @description : [投票组织信息]
 * @createTime : [2022/7/1 16:22]
 */
@Service
@Slf4j
public class VoteOrganizationService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public List<VoteOrganizationInfo> getOrgListByOrgCodeList(String orgTypeId, Collection<String> orgIdList) {
        Query orgQuery = new Query().addCriteria(Criteria.where("orgTypeId").is(orgTypeId).and("orgCode").in(orgIdList));
        return mongoTemplate.find(orgQuery, VoteOrganizationInfo.class);
    }

    public VoteOrganizationInfo getOrgInfoByOrgId(String orgTypeId, String orgId) {
        Query orgQuery = new Query().addCriteria(Criteria.where("orgId").is(orgId).and("orgTypeId").is(orgTypeId));
        return mongoTemplate.findOne(orgQuery, VoteOrganizationInfo.class);
    }

    public VoteOrganizationInfo getOrgInfoByOrgCode(String orgTypeId, String orgCode) {
        Query orgQuery = new Query().addCriteria(Criteria.where("orgTypeId").is(orgTypeId).and("orgCode").is(orgCode));
        return mongoTemplate.findOne(orgQuery, VoteOrganizationInfo.class);
    }

    public List<VoteOrganizationInfo> getOrgInfoByOrgName(String orgTypeId, String orgName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("orgName", orgName);
        queryWrapper.where("orgTypeId", orgTypeId);
        return mongoTemplate.find(queryWrapper, VoteOrganizationInfo.class);
    }

    /**
     * 增加组织票数
     * @param orgTypeId
     * @param orgCode
     * @return
     */
    public boolean updateOrgVote(String orgTypeId, String orgCode) {
        // 添加投票记录
        Update update = new Update();
        update.inc("realVoteNum", VoteConstant.DEFAULT_VOTE_NUM);
        update.inc("totalVoteNum", VoteConstant.DEFAULT_VOTE_NUM);
        Query orgQuery = new Query(Criteria.where("orgTypeId").is(orgTypeId).and("orgCode").is(orgCode));
        UpdateResult updateResult = mongoTemplate.updateFirst(orgQuery, update, VoteOrganizationInfo.class);
        return updateResult.getModifiedCount() > 0;
    }


    public Pair<Long, List<VoteOrganizationInfo>> getOrgList(OrgInfoSearchDTO dto) {
        QueryWrapper activeQuery = new QueryWrapper();
        activeQuery.where("id", dto.getActivityId());
        activeQuery.select("id", "orgTypeId");
        VoteSimpleActivityEntity activityInfo = mongoTemplate.findOne(activeQuery, VoteSimpleActivityEntity.class);
        if (activityInfo == null) {
            throw new BusinessException("活动信息不存在");
        }
        if (StringUtils.isBlank(dto.getKeyword())) {
            return Pair.of(0L, Collections.emptyList());
        }
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNumeric(dto.getKeyword())) {
            query.where("orgCode", dto.getKeyword());
        } else {
            query.like("orgName", dto.getKeyword());
        }
        long count = mongoTemplate.count(query, VoteOrganizationInfo.class);
        query.with(Sort.by(Sort.Order.desc("id")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<VoteOrganizationInfo> data = mongoTemplate.find(query.with(pageRequest), VoteOrganizationInfo.class);
        return Pair.of(count, data);
    }
}
